<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/12
 * Time: 11:26
 */
?>
<div class="box-body">
    <p>注：最少一行数据，最多10行数据</p>
    <div class="form-inline create_data" id="business">

    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-primary" id="business_btn">保存</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
    </div>
</div>

<script>
$(function(){
    var business_data = $("#business_input").text();
    if(business_data != '') {
        business_data = JSON.parse(business_data);
    }else{
        business_data = [];
    }
    var html = _.template($("#item_tpl").html());
    if ($("#business").find('.item_row').length == 0) {
        $("#business").append(html({data: business_data, sort: 0, down: false, up: false, plus: true, minus: false}));
    }
    $(document).on('click', '#business_btn', function (e) {
        var error = 0;
        var business = [];
        $("#business > .show").each(function (e) {
            var title = $(this).find(".title").val().replace(/(^\s*)|(\s*$)/, '');
            var num = $(this).find(".num").val();
            if (num != '' && (num < 1 || num.indexOf('.')>-1)) {
                alert('数量只能为正整数');
                return false;
            }
            if (title && num) {
                error ++;
                business.push({title: title, num: num});
            }
        })
        if (error > 0) {
            $("#business_input").text(JSON.stringify(business));
        }
        $("#formModal").modal('hide');
    })
})
</script>